package reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reggie.dto.DishDto;
import reggie.dto.SetmealDto;
import reggie.entity.*;
import reggie.mapper.SetmealMapper;
import reggie.service.CategoryService;
import reggie.service.SetmealDishService;
import reggie.service.SetmealService;

import java.util.ArrayList;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐，并将套餐菜品保存进setmael_dish表
     * @param setmealDto
     */
    @Transactional
    public void saveWithSetmealDishes(SetmealDto setmealDto) {

        //保存除套餐菜品之外的信息
        save(setmealDto);

        //获取菜品信息，但是没有套餐id需要遍历然后添加
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        if (!setmealDishes.isEmpty()) {
            for (int i = 0; i < setmealDishes.size(); i++) {

                SetmealDish setmealDish = new SetmealDish();
                setmealDish = setmealDishes.get(i);
                setmealDish.setSetmealId(setmealDto.getId());

                setmealDishes.set(i, setmealDish);
            }
        }

        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    public Page<SetmealDto> pageWithSetmealDishes(int page, int pageSize, String name) {

        //创建分页对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>();

        //条件构造器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        //添加过滤条件
        queryWrapper.like(name != null,Setmeal::getName,name);

        //添加排序条件
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        //执行分页查询
        page(pageInfo,queryWrapper);

        //对象拷贝
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");

        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> setmealDtos = new ArrayList<>();

        for (int i = 0; i < records.size(); i++) {

            Setmeal setmeal = records.get(i);
            SetmealDto setmealDto = new SetmealDto();

            BeanUtils.copyProperties(setmeal,setmealDto);

            Long categoryId = setmeal.getCategoryId();
            Category category = categoryService.getById(categoryId);

            setmealDto.setCategoryName(category.getName());

            setmealDtos.add(setmealDto);

        }

        setmealDtoPage.setRecords(setmealDtos);

        return setmealDtoPage;
    }

    /**
     * 根据setmealId查询对应setmealDto
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithSetmealDish(Long id) {

        SetmealDto setmealDto = new SetmealDto();

        Setmeal setmeal = getById(id);

        BeanUtils.copyProperties(setmeal,setmealDto);

        //根据SetmealId查询SetmealDish集合
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);

        //根据categoryId查询对应的category对象
        Long categoryId = setmeal.getCategoryId();
        Category category = categoryService.getById(categoryId);


        setmealDto.setSetmealDishes(setmealDishes);
        setmealDto.setCategoryName(category.getName());

        return setmealDto;
    }

    /**
     * 修改套餐以及对应的套餐菜品
     * @param setmealDto
     */
    @Transactional
    public void updateWithSetmealDish(SetmealDto setmealDto) {
        //更新除套餐菜品之外的信息
        updateById(setmealDto);

        //先删除之前的套餐菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());

        setmealDishService.remove(queryWrapper);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        for (int i = 0; i < setmealDishes.size(); i++) {

            SetmealDish setmealDish = setmealDishes.get(i);

            setmealDish.setSetmealId(setmealDto.getId());

            setmealDishes.set(i,setmealDish);
        }


        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    /**
     * 批量/单个删除
     * @param ids
     */
    public void deleteWithSetmealDish(List<Long> ids) {

        if (!ids.isEmpty()) {
            for (int i = 0; i < ids.size(); i++) {
                Long id = ids.get(i);

                //先根据SetmealId查询出相应的口味信息
                LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(SetmealDish::getSetmealId,id);

                //删除口味信息
                setmealDishService.remove(queryWrapper);

                //删除Setmeal表信息
                removeById(id);
            }
        }
    }
}
